<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="工单ID，使用uuid" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'workId', validatorRules.workId]" placeholder="请输入工单ID，使用uuid"></a-input>
        </a-form-item>
        <a-form-item label="工单编号【以g开头，拼接时间戳，再加5位数随机数】" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'workNo', validatorRules.workNo]" placeholder="请输入工单编号【以g开头，拼接时间戳，再加5位数随机数】"></a-input>
        </a-form-item>
        <a-form-item label="工单名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'workName', validatorRules.workName]" placeholder="请输入工单名称"></a-input>
        </a-form-item>
        <a-form-item label="客户名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'workCustomer', validatorRules.workCustomer]" placeholder="请输入客户名称"></a-input>
        </a-form-item>
        <a-form-item label="客户手机号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'workPhone', validatorRules.workPhone]" placeholder="请输入客户手机号"></a-input>
        </a-form-item>
        <a-form-item label="客户地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'workAddress', validatorRules.workAddress]" placeholder="请输入客户地址"></a-input>
        </a-form-item>
        <a-form-item label="客户反馈" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'workFeedback', validatorRules.workFeedback]" placeholder="请输入客户反馈"></a-input>
        </a-form-item>
        <a-form-item label="工单状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'workDistribution', validatorRules.workDistribution]" placeholder="请输入工单状态" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="工单开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择工单开始时间" v-decorator="[ 'workStart', validatorRules.workStart]" :trigger-change="true" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="工单结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择工单结束时间" v-decorator="[ 'workEnd', validatorRules.workEnd]" :trigger-change="true" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="工单紧急程度" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'workUrgent', validatorRules.workUrgent]" placeholder="请输入工单紧急程度" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="工单完成时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择工单完成时间" v-decorator="[ 'workComplete', validatorRules.workComplete]" :trigger-change="true" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="工单类型ID" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'workType', validatorRules.workType]" placeholder="请输入工单类型ID"></a-input>
        </a-form-item>
        <a-form-item label="创建人" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'createBy', validatorRules.createBy]" placeholder="请输入创建人"></a-input>
        </a-form-item>
        <a-form-item label="创建日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择创建日期" v-decorator="[ 'createTime', validatorRules.createTime]" :trigger-change="true" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="更新人" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'updateBy', validatorRules.updateBy]" placeholder="请输入更新人"></a-input>
        </a-form-item>
        <a-form-item label="更新日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择更新日期" v-decorator="[ 'updateTime', validatorRules.updateTime]" :trigger-change="true" style="width: 100%"/>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import JDate from '@/components/jeecg/JDate'  

  export default {
    name: "WorkOrderModal",
    components: { 
      JDate,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules:{
        workId:{rules: [{ required: true, message: '请输入工单ID，使用uuid!' }]},
        workNo:{rules: [{ required: true, message: '请输入工单编号【以g开头，拼接时间戳，再加5位数随机数】!' }]},
        workName:{rules: [{ required: true, message: '请输入工单名称!' }]},
        workCustomer:{},
        workPhone:{},
        workAddress:{},
        workFeedback:{},
        workDistribution:{rules: [{ required: true, message: '请输入工单状态' }]},
        workStart:{rules: [{ required: true, message: '请输入工单开始时间!' }]},
        workEnd:{rules: [{ required: true, message: '请输入工单结束时间!' }]},
        workUrgent:{rules: [{ required: true, message: '请输入工单紧急程度!' }]},
        workComplete:{},
        workType:{rules: [{ required: true, message: '请输入工单类型ID!' }]},
        createBy:{},
        createTime:{rules: [{ required: true, message: '请输入创建日期!' }]},
        updateBy:{},
        updateTime:{},
        },
        url: {
          add: "/worker/workOrder/add",
          edit: "/worker/workOrder/edit",
        }
     
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'workId','workNo','workName','workCustomer','workPhone','workAddress','workFeedback','workDistribution','workStart','workEnd','workUrgent','workComplete','workType','createBy','createTime','updateBy','updateTime'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'workId','workNo','workName','workCustomer','workPhone','workAddress','workFeedback','workDistribution','workStart','workEnd','workUrgent','workComplete','workType','createBy','createTime','updateBy','updateTime'))
      },

      
    }
  }
</script>